package pe.com.bbva.listeners;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import pe.com.bbva.configuracion.bo.ConfiguracionBO;
import pe.com.bbva.configuracion.domain.Configuracion;
import pe.com.bbva.targps.bo.UpdateQuartzBO;


public class iniQuartz implements ServletContextListener {
	private static final Logger logger = Logger.getLogger(iniQuartz.class);
    private static WebApplicationContext springContext;   
    
    @Override
	public void contextInitialized(ServletContextEvent event) {
    	try {
	        springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
	        UpdateQuartzBO updateQuartzBO=(UpdateQuartzBO)springContext.getBean("updateQuartzBO");
	        ConfiguracionBO configuracionBO=(ConfiguracionBO)springContext.getBean("configuracionBO");        
	        List<Configuracion> listaConfiguracion = new ArrayList<Configuracion>();	
	   		listaConfiguracion = configuracionBO.findConfiguracion();
			if(listaConfiguracion!=null && listaConfiguracion.size()>0 ) {				
				for (Configuracion configuracion:listaConfiguracion){					
					updateQuartzBO.actualizarConfiguracionQuartz(configuracion, null, true);					
				}
			}          
		} catch (Exception e) {
			logger.error("Exception actua cron:"+e.getMessage());
		} 
    }
    
    @Override
	public void contextDestroyed(ServletContextEvent event) {
    	
        springContext = null;
    }
    
    public static ApplicationContext getApplicationContext() {
        return springContext;
    }
}